﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace hxy.CopyRight
{
	/// <summary>
	/// 以文件存储的方式实现IRegisterInfoProvider
	/// </summary>
	public class FileRegisterInfoProvider : IRegisterInfoProvider
	{
		private string _filePath;

		public FileRegisterInfoProvider(string filePath)
		{
			if (string.IsNullOrWhiteSpace(filePath)) throw new ArgumentNullException("filePath");
			this._filePath = filePath;
		}

		public string GetRegisterInfo()
		{
			if (File.Exists(this._filePath))
			{
				var msg = File.ReadAllText(this._filePath, Encoding.Unicode);
				return msg;
			}

			return null;
		}

		public void SaveRegisterInfo(string persistentInfo)
		{
			if (string.IsNullOrWhiteSpace(persistentInfo)) throw new ArgumentNullException("persistentInfo");

			File.WriteAllText(this._filePath, persistentInfo, Encoding.Unicode);
			var fileInfo = new FileInfo(this._filePath);
			fileInfo.Attributes |= FileAttributes.Hidden;
		}

		public void Clear()
		{
			File.Delete(this._filePath);
		}
	}
}
